using System.Collections.Generic;
using Framework.StringSplit.Processors;

namespace Framework.StringSplit
{
    public class StringSplitHandler
    {
        public StringSplitHandler()
        {
            Trim = StringSplitTrimType.Trim;
            RemoveEmptyEntries = false;
            SeperatorChar = new[] {','};
            SeperatorInnerChar = null;
            SeperatorString = new string[0];
            Escape = '\\';
            EscapeInner = '~';
            Count = int.MaxValue;
        }

        //**************************************************************************************************************
        // Properties
        //**************************************************************************************************************

        public StringSplitTrimType Trim { get; set; }

        public bool RemoveEmptyEntries { get; set; }

        public int Count { get; set; }

        public bool IsCharSeperator
        {
            get
            {
                if (Is.EmptyArray(SeperatorString) && Is.EmptyArray(SeperatorChar))
                {
                    SeperatorChar = new[] {','};
                    return true;
                }

                return !Is.EmptyArray(SeperatorChar);
            }
        }

        private char[] _seperatorChar;
        public char[] SeperatorChar
        {
            get { return _seperatorChar; }
            set
            {
                _seperatorChar = value;
                _seperatorString = new string[0];
            }
        }

        public char[] SeperatorInnerChar { get; set; }

        private string[] _seperatorString;
        public string[] SeperatorString
        {
            get { return _seperatorString; }
            set
            {
                _seperatorString = value;
                _seperatorChar = new char[0];
            }
        }

        public char Escape { get; set; }

        public char EscapeInner { get; set; }

        //**************************************************************************************************************
        // Public Methods
        //**************************************************************************************************************

        public string[] Split(IStringSplitProcessor<string[]> processor, string text)
        {
            return processor.Process(this, text);
        }

        public List<string> SplitAsList(IStringSplitProcessor<List<string>> processor, string text)
        {
            return processor.Process(this, text);
        }

        public List<KeyValuePair<string, string>> SplitAsKeyValuePairList(IStringSplitProcessor<List<KeyValuePair<string, string>>> processor, string text)
        {
            return processor.Process(this, text);
        }

    }
}